package com.supcalls.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.supcalls.entity.Account;
import com.supcalls.mapper.AccountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Slf4j
public class AccountService {
    @Autowired
    private AccountMapper accountMapper;

    @Transactional
    public void decrease(Long userId, BigDecimal money) {
        log.info("扣减余额, userId={}, money={}", userId, money);
        Account account = accountMapper.selectOne(new QueryWrapper<Account>().eq("user_id", userId));
        account.setUsed(account.getUsed().add(money));
        account.setResidue(account.getResidue().subtract(money));
        accountMapper.updateById(account);
    }
}
